import requests
from bs4 import BeautifulSoup
import json

# 设置请求头，模拟浏览器访问
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.0 (KHTML, like Gecko) Chrome/120.0 Safari/537.36'
}

url = 'https://pvp.qq.com/web201605/herolist.shtml'

try:
    response = requests.get(url, headers=headers)
    response.encoding = 'gbk'  # 王者荣耀英雄列表页使用的是 GBK 编码！
    soup = BeautifulSoup(response.text, 'html.parser')

    # 获取英雄数据（页面中通过 JavaScript 注入，但实际数据在 hero_list.js 中）
    # 更可靠的方式：直接请求英雄 JSON 数据接口
    hero_api = 'https://pvp.qq.com/web201605/js/herolist.json'
    hero_res = requests.get(hero_api, headers=headers)
    hero_res.encoding = 'utf-8'
    heroes = hero_res.json()

    hero_data = []
    for hero in heroes:
        name = hero['cname']          # 中文名
        ename = hero['ename']         # 英雄ID
        title = hero.get('title', '') # 别名
        # 构造图片URL（官方图片命名规则）
        img_url = f'https://game.gtimg.cn/images/yxzj/img201606/heroimg/{ename}/{ename}.jpg'
        hero_data.append({
            'name': name,
            'title': title,
            'img_url': img_url
        })

    # 打印结果
    for h in hero_data:
        print(f"{h['name']} ({h['title']}) - {h['img_url']}")

    # 可选：保存为 JSON 文件
    with open('herolist.json', 'w', encoding='utf-8') as f:
        json.dump(hero_data, f, ensure_ascii=False, indent=2)

except Exception as e:
    print("Error:", e)